<html>
<head>
  <script src="OLLoader.js"></script>
  <script type="text/javascript">
  
    function test_initialize(t) {
        t.plan(4);
        
        var tileManager = new OpenLayers.TileManager();
        var map = new OpenLayers.Map('map', {
            zoomMethod: null,
            tileManager: tileManager
        });
        var layer = new OpenLayers.Layer.WMS('WMS1', '../img/blank.gif');
        map.addLayer(layer);
        map.setCenter([16, 48], 9);
        t.ok(tileManager.tileQueue[map.id].length, "Tiles queued from layer");
        map.removeLayer(layer);
        t.eq(tileManager.tileQueue[map.id].length, 0, "Tiles unqueued when layer is removed");
        map.addLayer(new OpenLayers.Layer.WMS('WMS2', '../img/blank.gif'));
        map.zoomIn();
        t.ok(tileManager.tileQueue[map.id].length, "Tiles queued from added layer");
        map.destroy();
        t.eq(tileManager.tileQueue[map.id], undefined, "Tile queue removed when map was destroyed");
    }
    
    function test_destroy(t) {
        t.plan(3);
        
        var tileManager = new OpenLayers.TileManager();
        var map = new OpenLayers.Map('map', {tileManager: tileManager});
        var layer = new OpenLayers.Layer.WMS('WMS', '../img/blank.gif');
        map.addLayer(layer);
        map.setCenter([16, 48], 9);
        var numTileListeners = layer.grid[0][0].events.listeners.beforeload.length;
        var numLayerListeners = layer.events.listeners.retile.length;
        var numMapListeners = map.events.listeners.preremovelayer.length;
        tileManager.destroy();
        t.eq(layer.grid[0][0].events.listeners.beforeload.length, numTileListeners - 1, "no listener on tile after destroy");
        t.eq(layer.events.listeners.retile.length, numLayerListeners - 1, "no listeners on layer after destroy");
        t.eq(map.events.listeners.preremovelayer.length, numMapListeners - 1, "no listeners on map after destroy");
        map.destroy();
    }

    function test_manageTileCache(t) {
        t.plan(10);
        
        var tileManager = new OpenLayers.TileManager({
            cacheSize: 12
        });
        var map = new OpenLayers.Map('map', {tileManager: tileManager});
        layer = new OpenLayers.Layer.WMS('WMS', '../img/blank.gif');
        map.addLayer(layer);
        map.setCenter([16, 48], 9);
        var gridSize;
        
        var firstInCache, sharedTile;
        t.delay_call(2, function() {
            t.eq(tileManager.tileCacheIndex.length, 12, "tiles cached");
            t.ok(~OpenLayers.Util.indexOf(tileManager.tileCacheIndex, layer.grid[1][2].url), "tile found in cache");
            t.ok(tileManager.tileCache[layer.grid[1][2].url] === layer.grid[1][2].imgDiv, "correct object cached");
            firstInCache = tileManager.tileCache[tileManager.tileCacheIndex[0]];
            sharedTile = tileManager.tileCache[tileManager.tileCacheIndex[11]];
            gridSize = layer.div.childNodes.length;
            map.setCenter([17, 47]);
        });

        function inCache(img) {
            var search = img.src.split('?')[1];
            for (var s in tileManager.tileCache) {
                if (s.split('?')[1] == search) {
                    return true;
                }
            }
            return false;
        }

        t.delay_call(4, function() {
            t.eq(tileManager.tileCacheIndex.length, 12, "tiles cached");
            t.ok(tileManager.tileCache[layer.grid[1][2].url] === layer.grid[1][2].imgDiv, "correct object cached");
            t.ok(!inCache(firstInCache), "old tile discarded");
            t.ok(inCache(sharedTile), "shared tile still in cache");
            firstInCache = tileManager.tileCache[tileManager.tileCacheIndex[0]];
            map.setCenter([16, 48]);
        });
        t.delay_call(6, function() {
            t.ok(!inCache(firstInCache), "old tile discarded");
            t.ok(inCache(sharedTile), "shared tile still in cache");
            t.eq(layer.div.childNodes.length, gridSize, 'no unused images left in dom');
            map.destroy();
        });
    }
    
    function test_queueTileDraw(t) {
        t.plan(3);

        var tileManager = new OpenLayers.TileManager();
        var map = new OpenLayers.Map('map', {tileManager: tileManager});
        layer = new OpenLayers.Layer.WMS('WMS', '../img/blank.gif');
        map.addLayer(layer);
        map.setCenter([0, 0], 3);
        var queued = tileManager.tileQueue[map.id].length;
        t.ok(tileManager.tileQueue[map.id].length, "Tiles queued for drawing");
        map.zoomIn();
        t.eq(tileManager.tileQueue[map.id].length, queued, "Tile queue has same length after immediate zoom change");
        t.delay_call(1, function() {
            t.eq(tileManager.tileQueue[map.id].length, 0, "Tiles from queue processed");        
            map.destroy();
        });
    }
    
    function test_deferTileDraw(t) {

        t.plan(3);

        var tileManager = new OpenLayers.TileManager();
        var map = new OpenLayers.Map('map', {tileManager: tileManager});
        layer = new OpenLayers.Layer.WMS('WMS', '../img/blank.gif');
        layer.destroy = function() {}; //we're going to do funky things with the grid
        layer.applyBackBuffer = function() {}; // backbuffering isn't under test here
        map.addLayer(layer);
        map.setCenter([-10, 0], 5);
        
        map.moveTo([5, 0]);
        t.ok(tileManager.tileQueue[map.id].length, "tile loading deferred after moveTo");
        map.moveTo([0, 0]);
        t.ok(tileManager.tileQueue[map.id].length, "deferred again after another moveTo");
        t.delay_call(1, function() {
            t.eq(tileManager.tileQueue[map.id].length, 0, "tiles loaded after moveDelay");
        });
    }
  </script>
</head>
<body>
<div id="map" style="width:499px;height:549px;display:none"></div>
</body>
</html>
